package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;
import java.util.Collection;

import ve.com.wayoyo.wuml_case.Painter;
import ve.com.wayoyo.wuml_case.UserPref;
import ve.com.wayoyo.wuml_case.DibujoDiagramas.CompConector._arrowType;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;

public class CompEntidad extends Componente {

	static final int textMargin = 15;
	public static final int UNA_SOLA_TABLA = 1;
	public static final int VARIAS_TABLAS = 2;

	private Paint _borderPaint = Painter.getInstancia().getDefaultBorderPaint();
	private RectF _border = new RectF();
	private String longestString;
	private int _tipoCase;
	private ArrayList<CompAtributoER> _atributos = new ArrayList<CompAtributoER>();
	private ArrayList<CompSubEntidad> _subEntidades = new ArrayList<CompSubEntidad>();
	private Boolean is_recursive = false;

	public CompEntidad(String Titulo) {
		this._tipoCase = 0;
		if (UserPref.getInstancia().isUsingConvections() && Titulo.length() > 1) {
			this.Titulo = Titulo.toUpperCase();
		} else {
			this.Titulo = Titulo;
		}
		allowRecursivity = true;
		_centerPoint = new Point();
		Point punto = new Point();
		punto.x = 20;
		punto.y = 20;
		this.setAnchor(punto);
		_width = Painter.getInstancia().getTextPaint(false, 1)
				.measureText(Titulo)
				+ textMargin;
		longestString = Titulo;
		_topAnchors = new BorderAnchors(4, Anchor, new Point(
				(int) (Anchor.x + _width), Anchor.y));
		_bottomAnchors = new BorderAnchors(4, new Point(Anchor.x,
				(int) (Anchor.y + _height)), new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y + _height)));
		_leftAnchors = new BorderAnchors(4, Anchor, new Point((int) (Anchor.x),
				(int) (Anchor.y + _height)));
		_rightAnchors = new BorderAnchors(4, new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y)), new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y + _height)));

		// Nuevo Metodo de Conexion
		_connectionPoints = new Point[16];
		_connectionPointsCost = new int[16];
		float cpDistX = _width / 5;
		float cpDistY = _height / 5;
		for (int i = 0; i < 4; i++) {
			_connectionPoints[i] = new Point(
					(int) (Anchor.x + ((i + 1) * cpDistX)), Anchor.y);
		}
		for (int i = 4; i < 8; i++) {
			_connectionPoints[i] = new Point((int) (Anchor.x + _width),
					(int) (Anchor.y + ((i - 3) * cpDistY)));
		}
		for (int i = 8; i < 12; i++) {
			_connectionPoints[i] = new Point(
					(int) ((Anchor.x + _width) - ((i - 7) * cpDistX)),
					(int) (Anchor.y + _height));
		}
		for (int i = 12; i < 16; i++) {
			_connectionPoints[i] = new Point(Anchor.x,
					(int) ((Anchor.y + _height) - ((i - 11) * cpDistY)));
		}
	}

	@Override
	public void Dibujar(Canvas c, boolean isFcoused, float globalScale,
			int offSetX, int offSetY) {

		int posX = Anchor.x + offSetX;
		int posY = Anchor.y + offSetY;
		Paint _textPaint = Painter.getInstancia().getTextPaint(false,
				globalScale);

		_width = _textPaint.measureText(longestString) / globalScale
				+ textMargin;

		// Dibujamos la linea del titulo
		// c.drawLine(posX*globalScale,(posY+30)*globalScale,(posX+_width)*globalScale,(posY+30)*globalScale,_borderPaint);

		// Dibujamos los atributos
		int puntoEscritura = 0;
		for (CompAtributoER value : _atributos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX + 5) * globalScale,
					(posY + 30 + puntoEscritura) * globalScale, _textPaint);
		}

		// Dibujamos Los conectores Padre
		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}

		// Dibujamos las subclases
		for (CompSubEntidad value : _subEntidades) {
			value.setAnchor(new Point((int) ((Anchor.x + 10)),
					(int) ((Anchor.y + 40 + puntoEscritura))));
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
			if ((value.get_width() + 20) > _width) {
				_width = (value.get_width() + 20);
			}
			puntoEscritura += value.get_height() + 15;
		}

		// Dibujamos el titulo
		float textSize = _textPaint.measureText(Titulo);
		float distToMid = (((_width) * globalScale) - textSize) / 2;
		c.drawText(Titulo, (posX * globalScale) + distToMid, (posY + 25)
				* globalScale, _textPaint);

		// Dibujamos el borde
		_border.set((posX) * globalScale, (posY) * globalScale, (posX + _width)
				* globalScale, (posY + _height) * globalScale);
		c.drawRoundRect(_border, 15 * globalScale, 15 * globalScale, Painter
				.getInstancia().getBorderPaint(_currentState));

	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,
			float GlobalScale) {
		boolean Visible = true;
		if ((Anchor.x + _width) <= offSetX) {
			Visible = false;
		} else if (Anchor.x <= offSetX + UserPref.getInstancia().getWinWidth()) {
			Visible = false;
		} else if (Anchor.y > offSetY + UserPref.getInstancia().getWinHeight()) {
			Visible = false;
		} else if ((Anchor.y + _height) <= offSetY) {
			Visible = false;
		}
		return Visible;
	}

	public Componente calcularTouchEntidad(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
				// FactoryPopup.getPopup(VistaDiagrama.getVista(), "Clases");
			}
		}
		if (respuesta) {
			for (CompSubEntidad value : _subEntidades) {
				if (value.calcularTouch(touch)) {
					return value;
				}
			}
			return this;
		}
		return null;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if ((touch.x <= Anchor.x + _width) && (touch.x >= Anchor.x)) {
			if ((touch.y <= Anchor.y + _height) && (touch.y >= Anchor.y)) {
				respuesta = true;
				// FactoryPopup.getPopup(VistaDiagrama.getVista(), "Clases");
			}
		}
		return respuesta;
	}

	@Override
	public CompConector calcularTouchConectores(Point touch) {
		for (CompConector value : _conectorsBegining) {
			if (value.calcularTouch(touch) == true)
				return value;
		}
		return null;
	}

	@Override
	public void actualizarAchorsPoints() {
		_topAnchors.actualizar(Anchor, new Point((int) (Anchor.x + _width),
				Anchor.y));
		_bottomAnchors.actualizar(new Point(Anchor.x,
				(int) (Anchor.y + _height)), new Point(
				(int) (Anchor.x + _width), (int) (Anchor.y + _height)));
		_leftAnchors.actualizar(Anchor, new Point((int) (Anchor.x),
				(int) (Anchor.y + _height)));
		_rightAnchors.actualizar(new Point((int) (Anchor.x + _width),
				(int) (Anchor.y)), new Point((int) (Anchor.x + _width),
				(int) (Anchor.y + _height)));

		float cpDistX = _width / 5;
		float cpDistY = _height / 5;
		for (int i = 0; i < 4; i++) {
			_connectionPoints[i].set((int) (Anchor.x + ((i + 1) * cpDistX)),
					Anchor.y);
		}
		for (int i = 4; i < 8; i++) {
			_connectionPoints[i].set((int) (Anchor.x + _width),
					(int) (Anchor.y + ((i - 3) * cpDistY)));
		}
		for (int i = 8; i < 12; i++) {
			_connectionPoints[i].set(
					(int) ((Anchor.x + _width) - ((i - 7) * cpDistX)),
					(int) (Anchor.y + _height));
		}
		for (int i = 12; i < 16; i++) {
			_connectionPoints[i].set((int) (Anchor.x),
					(int) ((Anchor.y + _height) - ((i - 11) * cpDistY)));
		}
		Diagrama diagramaActual = null;
		if (VistaDiagrama.getVista() != null) {
			diagramaActual = VistaDiagrama.getVista().getDiagrama();
			for (CompConector value : _conectorsEnd) {
				value.actualizarRuta(diagramaActual.getComponenteById(value
						.get_fatherId()));
			}
		}
		for (CompConector value : _conectorsBegining) {
			value.actualizarRuta(this);
		}
	}

	@Override
	public void actualizarPuntoCentral() {
		_width = Painter.getInstancia().getTextPaint(false, 1)
				.measureText(longestString) + 15;
		for (CompSubEntidad value : _subEntidades) {
			if ((value.get_width() + 20) > _width) {
				_width = (value.get_width() + 20);
			}
		}
		_centerPoint.set((Anchor.x + (int) (_width / 2)),
				(Anchor.y + (int) (_height / 2)));

	}

	@Override
	public String generarDOM() {
		String DOM = "\t<Componente Tipo=\"Entidad\" Titulo=\"" + Titulo
				+ "\" ID=\"" + ID + "\" AnchorX=\"" + Anchor.x
				+ "\" AnchorY=\"" + Anchor.y + "\" TipoCase=\"" + _tipoCase
				+ "\">\n";
		for (CompAtributoER value : _atributos) {
			DOM = DOM + value.generarDOM();
		}

		for (CompSubEntidad value : _subEntidades) {
			DOM = DOM + value.generarDOM();
		}

		DOM = DOM + "\t</Componente>\n";
		return DOM;
	}

	@Override
	public String getConectorsDOM() {
		String DOM = "";
		for (CompConector value : _conectorsBegining) {
			DOM = DOM + value.generarDOM();
		}
		for (CompSubEntidad value : _subEntidades) {
			DOM = DOM + value.getConectorsDOM();
		}
		return DOM;
	}

	@Override
	public void calculoLongitud() {
		Paint textPaint = Painter.getInstancia().getTextPaint(false, 1);
		_width = textPaint.measureText(Titulo) + textMargin;
		longestString = Titulo;
		_height = 80;
		if (_atributos.size() == 0) {
			_height += 15;
		} else {
			for (CompAtributoER atr : _atributos) {
				if (_width - textMargin < textPaint.measureText(atr.getText())) {
					_width = textPaint.measureText(atr.getText()) + textMargin;
					longestString = atr.getText();
				}
				_height += 30;
			}
		}
		for (CompSubEntidad Value : _subEntidades) {
			Value.calculoLongitud();
			_height += Value._height;
		}
		actualizarAchorsPoints();
		actualizarPuntoCentral();
	}

	@Override
	public void setTitulo(String titulo) {
		@SuppressWarnings("unused")
		Paint textPaint = Painter.getInstancia().getTextPaint(false, 1);
		if (UserPref.getInstancia().isUsingConvections()) {
			this.Titulo = titulo.toUpperCase();
		} else {
			this.Titulo = titulo;
		}
		calculoLongitud();
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	public void eliminarAtributo(int position) {
		this.get_atributos().remove(position);
		calculoLongitud();
	}

	public ArrayList<CompAtributoER> get_atributos() {
		return _atributos;
	}

	public void set_atributos(ArrayList<CompAtributoER> _atributos) {
		this._atributos = _atributos;
	}

	public void agregarSubClase(CompSubEntidad nuevo) {
		_subEntidades.add(nuevo);
		//_height = _height + nuevo._height + 20;}
		calculoLongitud();
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	@Override
	public void removeFocus() {
		for (CompSubEntidad Value : _subEntidades) {
			Value.removeFocus();
		}
		super.removeFocus();
	}

	public void addAtributo(CompAtributoER comp) {
		_atributos.add(comp);
		Paint textPaint = Painter.getInstancia().getTextPaint(false, 1);
		if (_width - textMargin < textPaint.measureText(comp.getText())) {
			_width = textPaint.measureText(comp.getText()) + textMargin;
			longestString = comp.getText();
		}
		_height += 30;
		actualizarPuntoCentral();
		actualizarAchorsPoints();
	}

	public ArrayList<String> getPKList() {
		ArrayList<String> Respuesta = new ArrayList<String>();
		for (CompAtributoER value : _atributos) {
			if (value.isPrimaryKey()) {
				Respuesta.add(value.getNombreVariable());
			}
		}
		return Respuesta;

		// String respuesta = "";
		// boolean firstPKflag = true;
		// for (CompAtributoER value : _atributos) {
		// if (value.isPrimaryKey()) {
		// if (firstPKflag) {
		// firstPKflag = false;
		// respuesta += value.getNombreVariable();
		// } else {
		// respuesta += ", " + value.getNombreVariable();
		// }
		// }
		// }
		// return respuesta;
	}

	public String getPKListEntity(CompEntidad entidad, String nullNotNull) {
		String streturn = "";
		String fkVar = "";

		for (CompAtributoER value : entidad.get_atributos()) {
			if (value.isPrimaryKey()) {
				fkVar = "FK_" + value.getNombreVariable() + "_"
						+ entidad.getTitulo() + " ";
				streturn = streturn + fkVar;
				streturn = streturn + value.getTipoDato() + " " + nullNotNull;
				streturn = streturn + ",\n";
				streturn = streturn + "CONSTRAINT FOREIGN KEY (" + fkVar
						+ ") REFERENCES " + entidad.getTitulo() + "("
						+ ((CompEntidad) entidad).getPKList() + ")\n";
			}
		}

		return null;
	}

	public String getCreate() {
		String query = "";
		if (_tipoCase == 0) {
			query = "CREATE TABLE " + Titulo + "\n(\n";
			boolean flag = false;
			for (CompAtributoER value : _atributos) {
				if (flag == false) {
					query += "\t" + value.getQueryDeclaration();
					flag = true;
				} else {
					query += ",\n\t" + value.getQueryDeclaration();
				}
			}
			ArrayList<String> pks = getPKList();
			String listPks = "";
			boolean firstPKflag = true;
			for (String value : pks) {
				if (firstPKflag) {
					firstPKflag = false;
					listPks += value;
				} else {
					listPks += ", " + value;
				}
			}

			if (listPks != "") {
				query += ",\n\tPRIMARY KEY(" + listPks + ")";
			}
			query += "\n);\n";
		} else if (_tipoCase == 1) {
			query = "CREATE TABLE " + Titulo + "\n(\n";
			boolean flag = false;
			for (CompAtributoER value : _atributos) {
				if (flag == false) {
					query += "\t" + value.getQueryDeclaration();
					flag = true;
				} else {
					query += ",\n\t" + value.getQueryDeclaration();
				}
			}
			for (CompSubEntidad value : _subEntidades) {
				for (CompAtributoER subvalue : value.get_atributos()) {
					if (flag == false) {
						query += "\t" + subvalue.getQueryDeclarationUnaTabla();
						flag = true;
					} else {
						query += ",\n\t"
								+ subvalue.getQueryDeclarationUnaTabla();
					}
				}
			}
			ArrayList<String> pks = getPKList();
			String listPks = "";
			boolean firstPKflag = true;
			for (String value : pks) {
				if (firstPKflag) {
					firstPKflag = false;
					listPks += value;
				} else {
					listPks += ", " + value;
				}
			}

			if (listPks != "") {
				query += ",\n\tPRIMARY KEY(" + listPks + ")";
			}
			query += "\n);\n";
		} else {
			for (CompSubEntidad value : _subEntidades) {
				boolean flag = false;
				query = query + "CREATE TABLE " + value.getTitulo() + "\n(\n";
				for (CompAtributoER valueEntity : _atributos) {
					if (flag == false) {
						query += "\t" + valueEntity.getQueryDeclaration();
						flag = true;
					} else {
						query += ",\n\t" + valueEntity.getQueryDeclaration();
					}
				}
				for (CompAtributoER subvalue : value.get_atributos()) {
					if (flag == false) {
						query += "\t" + subvalue.getQueryDeclaration();
						flag = true;
					} else {
						query += ",\n\t" + subvalue.getQueryDeclaration();
					}
				}
			}
		}
		return query;
	}

	public ArrayList<Object> getPKImportList() {
		EntidadCase entid = null;
		ArrayList<Object> list = new ArrayList<Object>();
		boolean flag = false;
		for (CompConector connectors : _conectorsBegining) {
			if ((connectors.get_fatherArrowType() == _arrowType.Asociacion_FK)
					|| (connectors.get_fatherArrowType() == _arrowType.PataDeGallo)
					|| (connectors.get_fatherArrowType() == _arrowType.PataDeGallo_PK)) {
				entid = new EntidadCase();
				entid.entidad = (CompEntidad) VistaDiagrama.getVista()
						.getDiagrama()
						.getComponenteById(connectors.get_childId());
				if (connectors.get_childId() == connectors.get_fatherId()) {
					entid.entidad.setIs_recursive(true);
				}
				if (connectors.get_fatherArrowType() == _arrowType.PataDeGallo_PK)
					entid.debil = true;
				else
					entid.debil = false;
				if ((connectors.get_currentLineType() == CompConector.NORMAL_TYPE)
						|| (connectors.get_currentLineType() == CompConector.CHILD_SIDE_DASHED)) {
					entid.NotNull = true;
				} else
					entid.NotNull = false;
				list.add(entid);
				flag = true;
			}
		}
		for (CompConector connectors : _conectorsEnd) {
			if ((connectors.get_childArrowType() == _arrowType.Asociacion_FK)
					|| (connectors.get_childArrowType() == _arrowType.PataDeGallo)
					|| (connectors.get_childArrowType() == _arrowType.PataDeGallo_PK)) {
				entid = new EntidadCase();
				entid.entidad = (CompEntidad) VistaDiagrama.getVista()
						.getDiagrama()
						.getComponenteById(connectors.get_fatherId());
				if (connectors.get_childId() == connectors.get_fatherId()) {
					entid.entidad.setIs_recursive(true);
				}
				if (connectors.get_childArrowType() == _arrowType.PataDeGallo_PK)
					entid.debil = true;
				else
					entid.debil = false;
				if ((connectors.get_currentLineType() == CompConector.NORMAL_TYPE)
						|| (connectors.get_currentLineType() == CompConector.FATHER_SIDE_DASHED))
					entid.NotNull = true;
				else
					entid.NotNull = false;
				list.add(entid);
				flag = true;
			}
		}
		ArrayList<Object> listPk = new ArrayList<Object>();
		for (CompAtributoER object : this.get_atributos()) {
			if (object.isPrimaryKey()) {
				listPk.add(object);
			}
		}
		if (!flag) {
			return listPk;
		} else {
			for (Object compEntidad : list) {
				ArrayList<Object> listEntidad = new ArrayList<Object>();
				if (!(((EntidadCase) compEntidad).entidad.getIs_recursive())) {
					listEntidad.addAll(((EntidadCase) compEntidad).entidad
							.getPKImportList());
					for (Object object : listEntidad) {
						CompAtributoER atr = (CompAtributoER) object;
						try {
							atr = atr.clone();
							atr.setNombreVariable(atr.getNombreVariable()
									+ "_"
									+ ((EntidadCase) compEntidad).entidad
											.getTitulo());
							atr.setUnique(false);
							if (((EntidadCase) compEntidad).debil) {
								atr.setPrimaryKey(true);
							} else {
								atr.setPrimaryKey(false);
							}
							if (((EntidadCase) compEntidad).NotNull) {
								atr.setNull(false);
							} else {
								atr.setNull(true);
							}
							Boolean flag2 = false;
							for (CompAtributoER atributo : this.get_atributos()) {
								if (atributo.getNombreVariable().equals(
										atr.getNombreVariable()))
									flag2 = true;
							}
							if (!flag2) {
								this.get_atributos().add(atr);
							}

						} catch (Exception e) {
						}
					}
				}
			}
			return listPk;
		}
	}

	public String getAlterMySQL() {
		String streturn = "";
		Boolean flagAlter = false;
		Boolean flagJump = false;
		Boolean flagControl = false;
		int control = 1;
		for (CompConector connectors : _conectorsBegining) {
			if ((connectors.get_fatherArrowType() == _arrowType.Asociacion_FK)
					|| (connectors.get_fatherArrowType() == _arrowType.PataDeGallo)
					|| (connectors.get_fatherArrowType() == _arrowType.PataDeGallo_PK)) {
				Componente comp = VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(connectors.get_childId());
				int i = 0;
				for (CompAtributoER atrib : ((CompEntidad) comp)._atributos) {
					if (atrib.isPrimaryKey()) {
						
						if (flagJump) {
							streturn = streturn + ",\n";
						}
						String ident = "fk_"+this.getTitulo()+"_"+comp.getTitulo();
						
						if (ident.length()>30)
						{
							if (flagControl){
								ident = ident.substring(0, 28)+String.valueOf(control);
								control ++;
							}else{
								ident = ident.substring(0, 29);
								flagControl=true;
							}
						}
						streturn = streturn + "CONSTRAINT "+ident+" FOREIGN KEY ("
								+ atrib.getNombreVariable() + "_"
								+ comp.getTitulo() + ") REFERENCES "
								+ comp.getTitulo().toUpperCase() + "("
								+ ((CompEntidad) comp).getPKList().get(i) + ")";
						i++;
						flagJump = true;
						flagAlter = true;
					}
				}
			}
		}
		for (CompConector connectors : _conectorsEnd) {
			if ((connectors.get_childArrowType() == _arrowType.Asociacion_FK)
					|| (connectors.get_childArrowType() == _arrowType.PataDeGallo)
					|| (connectors.get_childArrowType() == _arrowType.PataDeGallo_PK)) {
				Componente comp = VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(connectors.get_fatherId());

				int i = 0;
				for (CompAtributoER atrib : ((CompEntidad) comp)._atributos) {
					if (atrib.isPrimaryKey()) {
						if (flagJump) {
							streturn = streturn + ",\n";
						}
						String ident = "fk_"+this.getTitulo()+"_"+comp.getTitulo();
						if (ident.length()>30)
						{
							if (flagControl){
								ident = ident.substring(0, 28)+String.valueOf(control);
								control ++;
							}else{
								ident = ident.substring(0, 29);
								flagControl=true;
							}
						}
						streturn = streturn + "CONSTRAINT "+ident+" FOREIGN KEY ("
								+ atrib.getNombreVariable() + "_"
								+ comp.getTitulo() + ") REFERENCES "
								+ comp.getTitulo().toUpperCase() + "("
								+ ((CompEntidad) comp).getPKList().get(i) + ")";
						flagJump = true;
						flagAlter = true;

					}
				}
			}
		}

		if (flagAlter) {
			if (UserPref.getInstancia().getLanguaje().equals("sqlServer")){
				streturn = "\n\nALTER TABLE " + getTitulo() +						 
						" ADD \n "+
						streturn +
						";"
						;	
			}else{
				streturn = "\n\nALTER TABLE " + getTitulo() + 
						" ADD(\n"+						
						streturn +
						");"						
						;
			}
		}
		return streturn;
	}

	public ArrayList<CompSubEntidad> get_subEntidades() {
		return _subEntidades;
	}

	@Override
	public String getTipo() {
		return "Entidad";
	}

	public Boolean validAtribNew(CompAtributoER nuevo) {
		for (CompAtributoER atrib : this._atributos) {
			if (atrib.getNombreVariable().equals(nuevo.getNombreVariable())) {
				return false;
			}
		}
		for (CompSubEntidad subentity : this._subEntidades) {
			for (CompAtributoER subatr : subentity.get_atributos()) {
				if (subatr.getNombreVariable()
						.equals(nuevo.getNombreVariable())) {
					return false;
				}
			}
		}
		return true;
	}

	public Boolean validAtrib(CompAtributoER atr) {
		Boolean flag = false;
		for (CompAtributoER atrib : this._atributos) {
			if (atrib.getNombreVariable().equals(atr.getNombreVariable())) {
				if (!flag)
					flag = true;
				else
					return false;
			}
		}
		for (CompSubEntidad subentity : this._subEntidades) {
			for (CompAtributoER subatr : subentity.get_atributos()) {
				if (subatr.getNombreVariable().equals(atr.getNombreVariable())) {
					if (!flag)
						flag = true;
					else
						return false;
				}
			}

		}
		return true;
	}

	public boolean ExistSubClase() {
		if (_subEntidades.size() > 0)
			return true;
		return false;
	}

	public void setTipoCase(int TipoCase) {
		this._tipoCase = TipoCase;
	}

	public int getTipoCase() {
		return this._tipoCase;
	}

	public ArrayList<CompEntidad> separarSubEntidad(Diagrama diag) {
		ArrayList<CompEntidad> listReturn = new ArrayList<CompEntidad>();
		for (CompSubEntidad value : this.get_subEntidades()) {
			CompEntidad entidad = new CompEntidad(value.getTitulo());
			value.get_atributos().addAll(this.get_atributos());
			// entidad.get_atributos().addAll(value.get_atributos());
			entidad.set_atributos(value.get_atributos());
			entidad.setID(diag.get_lastIdGiven());
			
			for (CompConector connectors : _conectorsBegining) {
				CompConector clone = connectors.clone();
				clone.set_fatherId(entidad.getID());
				entidad.get_conectorsBegining().add(clone);
				Componente comp = VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(connectors.get_childId());
				comp.get_conectorsEnd().remove(connectors);
				comp.get_conectorsEnd().add(clone);
			}
			for (CompConector connectors : _conectorsEnd) {
				CompConector clone = connectors.clone();
				clone.set_childId(entidad.getID());
				entidad.get_conectorsEnd().add(clone);
				Componente comp = VistaDiagrama.getVista().getDiagrama()
						.getComponenteById(connectors.get_fatherId());
				comp.get_conectorsBegining().remove(connectors);
				comp.get_conectorsBegining().add(clone);
			}
			listReturn.add(entidad);
		}
		return listReturn;
	}

	public Boolean getIs_recursive() {
		return is_recursive;
	}

	public void setIs_recursive(Boolean is_recursive) {
		this.is_recursive = is_recursive;
	}
}

class EntidadCase {
	CompEntidad entidad;
	Boolean NotNull;
	Boolean debil;
}
